package com.example.himalayaproject.subscription.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.subscription.adapter.FragmentSepcialProgramAdapter;
import com.example.himalayaproject.subscription.bean.programentity.ProgramEntity;
import com.example.himalayaproject.subscription.callback.HttpSercive;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class SpecialProgramFragment extends Fragment implements Callback<ProgramEntity>{
    private FragmentSepcialProgramAdapter adapter;
    private int  number = 0;
    private HttpSercive sercive;
    private  String albumId;
    private int page = 20 ;
    private Button button;

    public static SpecialProgramFragment newInstance(String albumId,int number) {
        Bundle args = new Bundle();
        args.putString("albumId", albumId);
        args.putInt("number",number);
        SpecialProgramFragment fragment = new SpecialProgramFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_special_program, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        aboutRecyclerView(view);
        aboutDowmLoadData(view);
    }

    private void aboutRecyclerView(View view) {
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.fragment_special_program_recycler);
        TextView textView = (TextView) view.findViewById(R.id.fragment_special_program_number_tv);
        button = (Button) view.findViewById(R.id.fragment_special_program_item_button2);
         number = getArguments().getInt("number");

        textView.setText("共"+number+"集");
        adapter = new FragmentSepcialProgramAdapter(getContext());
        recyclerView.setAdapter(adapter);
    }

    private void aboutDowmLoadData(View view) {
        albumId = getArguments().getString("albumId");
       sercive = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpSercive.class);
        sercive.getProgram(albumId,"android",null, number).enqueue(this);
    }

    @Override
    public void onResponse(Call<ProgramEntity> call, Response<ProgramEntity> response) {
        adapter.addAll(response.body().getData().getTracks().getList());
    }
    @Override
    public void onFailure(Call<ProgramEntity> call, Throwable t) {

    }
}
